package dmsxl.zhanyuduilie;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Author: Zhang Dongwei
 * Date: 2023/5/3 19:47
 * 用队列实现栈
 */
public class zydl3_225 {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        System.out.println(myStack.top());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
    }
}

//两个queue
//class MyStack {
//
//    private static Queue<Integer> queue1;
//    private static Queue<Integer> queue2;
//
//    public MyStack() {
//        queue1 = new LinkedList<>();
//        queue2 = new LinkedList<>();
//    }
//
//    public void push(int x) {
//        queue2.offer(x);
//        while (!queue1.isEmpty()){
//            queue2.offer(queue1.poll());
//        }
//        Queue<Integer> queueTemp;
//        queueTemp = queue1;
//        queue1 = queue2;
//        queue2 = queueTemp;
//    }
//
//    public int pop() {
//        return queue1.poll();
//    }
//
//    public int top() {
//        return queue1.peek();
//    }
//
//    public boolean empty() {
//        return queue1.isEmpty();
//    }
//}

//一个queue
class MyStack {

    private static Queue<Integer> queue;

    public MyStack() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.offer(x);
        int size = queue.size();
        if (size-- >1){
            queue.offer(queue.poll());
        }
    }

    public int pop() {
        return queue.poll();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}